package com.aspire.boc.mms.wmf.util;

import java.io.Serializable;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.aspire.boc.mms.wmf.constants.GlobalConstant;

/**
 * 粗、细粒度权限管理:
 *    权限数据加载、数据获取与检测api
 *
 * @date 2014年8月4日 下午11:40:28 
 * @author Zhanghao
 *
 */
@SuppressWarnings("serial")
public class WmfPermissionManage implements Serializable {
    
    private Log logger = LogFactory.getLog(WmfPermissionManage.class);
    
    private final static String SESSION_KEY = "WMF_SESSION_PERMISSION_MANAGE";
    
    private static PermissionConfig permissionConfig;
    
    static {
        String url = Configuration.getConfiguration("system.permission.url");
        String systemDef = Configuration.getConfiguration("system.permission.system_def");
        String userpermsMethod = Configuration.getConfiguration("system.permission.userperms_method");
        permissionConfig = new PermissionConfig(url, systemDef, userpermsMethod);
    }
    
    private boolean isSystemAdmin;
    
    private WmfPermissionManage() {
        
        super();
    }
    
    /**
     * 是否为系统管理员
     * @return
     */
    public boolean isSystemAdmin() {
        
        return isSystemAdmin;
    }
    
    /**
     * 获取session保存的细粒度权限管理器
     * @param request
     * @return
     */
    public static WmfPermissionManage getSessionInstance(HttpSession session) {
        
        WmfPermissionManage instance = (WmfPermissionManage) session.getAttribute(SESSION_KEY);
        return instance!=null?instance:new WmfPermissionManage();
    }
    
    /**
     * 保存细粒度权限管理器至session
     * @param request
     */
    public static void setSessionInstance(HttpSession session, String userId) {
        
        session.setAttribute(SESSION_KEY, getSessionInstance(session).loadPerms(userId));
    }
    
    /**
     * 加载细粒度权限数据
     * 
     * @param userId
     */
    private WmfPermissionManage loadPerms(String userId) {
        
        // 判断wmf管理员权限
        loadAdminPerm(userId, permissionConfig);
        return this;
    }
    
    /**
     * 加载当前用户管理员角色信息
     * 
     * @param userId
     * @param permissionConfig
     */
    private void loadAdminPerm(String userId, PermissionConfig permissionConfig) {
        
        // 获取用户角色
        Vector<Object> params = new Vector<Object>();
        params.add(permissionConfig.getSystemDef());
        params.add(userId);
        Vector<?> permList = null;
        try {
            permList  = (Vector<?>) PermissionUtil.execXmlRpc(permissionConfig.getUrl(), permissionConfig.getMethod(), params);
            if(permList!=null && permList.size()>0) {
                for (Object obj : permList) {
                    @SuppressWarnings("unchecked")
                    Map<String, ?> map = (Map<String, Object>)obj;
                    String res_string = (String) map.get("res_string");
                    // 判断是否为系统管理员
                    if (GlobalConstant.RES_STRING_OF_SYSTEM_ADMIN.equals(res_string)) {
                        isSystemAdmin = true;
                        break;
                    }
                }
            }
            logger.info(">>>>>>>>>>success to load user["+userId+"] role data, role info: [SystemAdmin:"+isSystemAdmin+"]");
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(">>>>>>>>>>fail to load user["+userId+"], details : "+e.getMessage());
        }
        
    }

}
